<template>
  <el-dialog v-model="deleteVisible" width="50%" align-center class="Vdialog" @close="cancel">
    <template #header>
      <h4 class="dialogName">{{ title }}</h4>
    </template>
    <div class="content">
      <el-form ref="ruleFormRef" :model="ruleForm.role" :rules="rules" label-position="top" class="demo-ruleForm">
        <el-form-item label="角色" prop="name">
          <el-input v-model="ruleForm.role.name" placeholder="角色名称" class="input-switch" />
          <el-switch v-model="ruleForm.role.status" inline-prompt active-text="启用" inactive-text="禁用" :active-value='1'
            :inactive-value='0' />
        </el-form-item>
      </el-form>
      <el-tabs type="border-card" class="tab-pane">
        <el-tab-pane label="菜单权限">
          <el-cascader-panel v-model="ruleForm.role_owner.menu_ids" :options="menuOptions" :props="cascaderProps"
            class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="按钮授权">
          <el-cascader-panel v-model="ruleForm.role_owner.button_ids" :options="buttonOptions" :props="cascaderProps"
            class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="数据权限">
          <el-cascader-panel v-model="ruleForm.role_owner.data_user_ids" :options="options" :props="cascaderProps"
            class="cascaderPanel" />
        </el-tab-pane>
        <el-tab-pane label="属性权限">
          <el-cascader-panel v-model="ruleForm.role_owner.field_attr_ids" :options="attributeOptions"
            :props="cascaderProps" class="cascaderPanel" />
        </el-tab-pane>
      </el-tabs>

    </div>
    <template #footer>
      <div class="dialog-button">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirm" :loading="loading.save">
          保存
        </el-button>
        <el-button type="primary" @click="confirmEnable" :loading="loading.saveEnable">
          保存并启用
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="VRole">
import { organizationList } from "@/api/system/role"
import { getMenus } from "@/api/menu"
import { reactive, ref, toRefs, watch } from 'vue'
const emits = defineEmits(['cancel', 'confirm'])
// 声明接收参数
const props = defineProps({
  title: {
    type: String,
    default: "新增"
  },
  formData: {
    type: Object,
    default: {
      role: {
        name: '',
        status: ''
      },
      role_owner: {
        menu_ids: [],
        field_attr_ids: [],
        button_ids: [],
        data_user_ids: []
      }
    }
  }
})
const data = reactive({
  deleteVisible: false,
  ruleForm: {
    role: {
      name: '',
    },
    role_owner: {
      menu_ids: [],
      field_attr_ids: [],
      button_ids: [],
      data_user_ids: []
    }
  },
  loading: {},
  cascaderProps: {
    emitPath: false,
    multiple: true,
    label: 'name',
    value: 'id',
    children: 'sub'
  },
  options: [],
  menuOptions: [],
  buttonOptions: [],
  attributeOptions: []
})

const ruleFormRef = ref()
const { deleteVisible, ruleForm, loading, cascaderProps, options, attributeOptions, buttonOptions, menuOptions } = toRefs(data)
const { formData } = toRefs(props)
const rules = reactive({
  name: [
    { required: true, message: '角色名称不能为空', trigger: 'blur' },
  ],
})
watch(() => formData.value, (newVal, oldVal) => {
  ruleForm.value = JSON.parse(JSON.stringify(newVal))
})
const open = async () => {
  await organizationList().then(res => {
    options.value = res.data
  })
  await getMenus({ type: 1 }).then(res => {
    menuOptions.value = res.data
  })
  await getMenus({ type: 2 }).then(res => {
    buttonOptions.value = res.data
  })
  deleteVisible.value = true

}
const confirm = () => {
  let { validate } = ruleFormRef.value
  validate((valid) => {
    if (valid) {
      const obj = JSON.parse(JSON.stringify(ruleForm.value))
      obj.role.status = Number(obj.role.status)
      emits('confirm', obj)
      loading.value.save = true
      loading.value.saveEnable = true
    }
  })
}
const cancel = () => {
  ruleForm.value = {
    role: {
      name: '',
    },
    role_owner: {
      menu_ids: [],
      field_attr_ids: [],
      button_ids: [],
      data_user_ids: []
    }
  }
  deleteVisible.value = false
  loading.value.saveEnable = false
  loading.value.save = false
  emits('cancel')
}
const confirmEnable = () => {
  ruleForm.value.role.status = true
  confirm()
}
defineExpose({
  open, data, cancel
})
</script>
<style lang="scss" scoped>
.content {
  align-items: center;

  span {
    margin-left: 5px;
  }
}

.dialogName {
  font-size: 16px;
  font-weight: bold;
}

.input-switch {
  width: calc(100% - 58px);
  margin-right: 10px;
}

:deep(.tab-pane) {
  margin: 10px 20px;
  border-radius: 10px;

  .el-tabs__header {
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
  }

  .el-tabs__nav {
    width: 100%;
    border-radius: 10px;
  }

  .el-tabs__item.is-active {
    color: #333;
    background-color: #fafafa;
  }

  .el-tabs__item {
    margin-left: 0 !important;
    width: 25% !important;
    color: #fff;
    text-align: center;
    border: none;
    border-right: 1px solid #fff;
    background: #6d7282;

    &:last-child {
      border-right: none;
    }
  }

  .el-tabs__item:first-child {
    border-top-left-radius: 10px;
  }

  .el-tabs__item:last-child {
    text-align: center;
    border-top-right-radius: 10px;
  }
}

.cascaderPanel {
  overflow: auto;
  height: 400px;

  &:deep(.el-scrollbar__wrap) {
    height: 100% !important;
  }
}
</style>